﻿using System;
using System.Collections.Generic;
using MTC.Common.DTO;
using MTC.Web.Business.DTO;
using Driver = MTC.Business.Driver.Driver;

namespace MTC.Web.Business
{
    public class Drivers
    {
        private List<DriverEntity> _driverEntityList = new List<DriverEntity>();
        
        public List<DriverEntity> GetDrivers()
        {
            List<MTC.Common.DTO.Driver> drivers;
            drivers = Driver.GetDrivers();
            if (drivers == null)
                return null;
            _driverEntityList.Clear();
            foreach (MTC.Common.DTO.Driver driver in drivers)
            {
                DriverEntity entity = new DriverEntity();
                entity.DriverId = driver.DriverId;
                entity.FirstName = driver.FirstName;
                entity.LastName = driver.LastName;
                entity.Mobile1 = driver.Mobile1;
                entity.Mobile2 = driver.Mobile2;
                entity.TaxiLicience = driver.TaxiLicience;
                entity.TaxiLicienceExpiry = driver.TaxiLicienceExpiry;


                _driverEntityList.Add(entity);
            }
            return _driverEntityList;
        }

        public DriverEntity GetADriver(int driverid )
        {
            if (driverid == 0)
                return null;
            DriverEntity entity = null;
            DriverRateEntity rateEntity = null;
            MTC.Common.DTO.Driver driver = Driver.GetDriver(driverid);
            if (driver != null)
            {
                entity = new DriverEntity();
                entity.DriverId = driver.DriverId;
                entity.FirstName = driver.FirstName;
                entity.LastName = driver.LastName;
                entity.Mobile1 = driver.Mobile1;
                entity.Mobile2 = driver.Mobile2;
                entity.TaxiLicience = driver.TaxiLicience;
                entity.TaxiLicienceExpiry = driver.TaxiLicienceExpiry;
                entity.ABN = driver.ABN;
                entity.Licience = driver.Licience;
                entity.ETagAccount = driver.ETagAccount;
                entity.ETagNumber = driver.ETagNumber;
                entity.Nickname = driver.Nickname;
                entity.Title = driver.Title;
                entity.Gender = driver.Gender;
                entity.Street1 = driver.Street1;
                entity.Street2 = driver.Street2;
                entity.Suburb = driver.Suburb;
                entity.State = driver.State;
                entity.Postcode = driver.Postcode;
                entity.Phone = driver.Phone;
                entity.DateOfBirth = driver.DateOfBirth;
                entity.CommencementDate = driver.CommencementDate;
                entity.DriverLicienceExpiry = driver.DriverLicienceExpiry;
                
                rateEntity = new DriverRateEntity();
                entity.Rate = rateEntity;
                rateEntity.DriverId = driver.DriverId;
                rateEntity.DriverRateId = driver.Rates.DriverRateId;
                rateEntity.MonDayRate = driver.Rates.MonDayRate;
                rateEntity.MonNightRate = driver.Rates.MonNightRate;
                rateEntity.TuesDayRate = driver.Rates.TuesDayRate;
                rateEntity.TuesNightRate = driver.Rates.TuesNightRate;
                rateEntity.WedDayRate = driver.Rates.WedDayRate;
                rateEntity.WedNightRate = driver.Rates.WedNightRate;
                rateEntity.ThursDayRate = driver.Rates.ThursDayRate;
                rateEntity.ThursNightRate = driver.Rates.ThursNightRate;
                rateEntity.FriDayRate = driver.Rates.FriDayRate;
                rateEntity.FriNightRate = driver.Rates.FriNightRate;
                rateEntity.SatDayRate = driver.Rates.SatDayRate;
                rateEntity.SatNightRate = driver.Rates.SatNightRate;
                rateEntity.SunDayRate = driver.Rates.SunDayRate;
                rateEntity.SunNightRate = driver.Rates.SunNightRate;

            }
            return entity;
        }

        public bool UpdateADriver(DriverEntity entity)
        {
            if (entity==null)
            {
                return false;
            }
            MTC.Common.DTO.Driver driver = new Common.DTO.Driver();
            MTC.Common.DTO.DriverRate driverRate = new DriverRate();

            driver.DriverId = entity.DriverId;
            driver.FirstName = entity.FirstName;
            driver.LastName = entity.LastName;
            driver.Mobile1 = entity.Mobile1;
            driver.Mobile2 = entity.Mobile2;
            driver.TaxiLicience = entity.TaxiLicience;
            driver.TaxiLicienceExpiry = entity.TaxiLicienceExpiry;
            driver.ABN = entity.ABN;
            driver.Licience = entity.Licience;
            driver.ETagAccount = entity.ETagAccount;
            driver.ETagNumber = entity.ETagNumber;
            driver.Nickname = entity.Nickname;
            driver.Title = entity.Title;
            driver.Gender = entity.Gender;
            driver.Street1 = entity.Street1;
            driver.Street2 = entity.Street2;
            driver.Suburb = entity.Suburb;
            driver.State = entity.State;
            driver.Postcode = entity.Postcode;
            driver.Phone = entity.Phone;
            driver.DateOfBirth = entity.DateOfBirth;
            driver.CommencementDate = entity.CommencementDate;
            driver.DriverLicienceExpiry = entity.DriverLicienceExpiry;
            driver.TaxiLicienceExpiry = entity.TaxiLicienceExpiry;
            driver.DriverLicienceExpiry = entity.DriverLicienceExpiry;

            driver.Rates = driverRate;

            driverRate.DriverId = entity.DriverId;
            driverRate.DriverRateId = entity.Rate.DriverRateId;
            driverRate.MonDayRate = entity.Rate.MonDayRate;
            driverRate.MonNightRate = entity.Rate.MonNightRate;
            driverRate.TuesDayRate = entity.Rate.TuesDayRate;
            driverRate.TuesNightRate = entity.Rate.TuesNightRate;
            driverRate.WedDayRate = entity.Rate.WedDayRate;
            driverRate.WedNightRate = entity.Rate.WedNightRate;
            driverRate.ThursDayRate = entity.Rate.ThursDayRate;
            driverRate.ThursNightRate = entity.Rate.ThursNightRate;
            driverRate.FriDayRate = entity.Rate.FriDayRate;
            driverRate.FriNightRate = entity.Rate.FriNightRate;
            driverRate.SatDayRate = entity.Rate.SatDayRate;
            driverRate.SatNightRate = entity.Rate.SatNightRate;
            driverRate.SunDayRate = entity.Rate.SunDayRate;
            driverRate.SunNightRate = entity.Rate.SunNightRate;

            return Driver.UpdateDriver(driver);


        }

        public bool DeleteADriver(int DriverId)
        {
            if (DriverId == 0)
                return false;

            return Driver.DeleteDriver(DriverId);
        }

        public int AddDriver(DriverEntity entity)
        {
            if (entity == null)
            {
                return 0;
            }
            MTC.Common.DTO.Driver driver = new Common.DTO.Driver();
            MTC.Common.DTO.DriverRate driverRate = new DriverRate();

            driver.DriverId = entity.DriverId;
            driver.FirstName = entity.FirstName;
            driver.LastName = entity.LastName;
            driver.Mobile1 = entity.Mobile1;
            driver.Mobile2 = entity.Mobile2;
            driver.TaxiLicience = entity.TaxiLicience;
            driver.TaxiLicienceExpiry = entity.TaxiLicienceExpiry;
            driver.ABN = entity.ABN;
            driver.Licience = entity.Licience;
            driver.ETagAccount = entity.ETagAccount;
            driver.ETagNumber = entity.ETagNumber;
            driver.Nickname = entity.Nickname;
            driver.Title = entity.Title;
            driver.Gender = entity.Gender;
            driver.Street1 = entity.Street1;
            driver.Street2 = entity.Street2;
            driver.Suburb = entity.Suburb;
            driver.State = entity.State;
            driver.Postcode = entity.Postcode;
            driver.Phone = entity.Phone;
            driver.DateOfBirth = entity.DateOfBirth;
            driver.CommencementDate = entity.CommencementDate;
            driver.DriverLicienceExpiry = entity.DriverLicienceExpiry;
            driver.TaxiLicienceExpiry = entity.TaxiLicienceExpiry;
            driver.DriverLicienceExpiry = entity.DriverLicienceExpiry;

            driver.Rates = driverRate;

            driverRate.DriverId = entity.DriverId;
            driverRate.DriverRateId = entity.Rate.DriverRateId;
            driverRate.MonDayRate = entity.Rate.MonDayRate;
            driverRate.MonNightRate = entity.Rate.MonNightRate;
            driverRate.TuesDayRate = entity.Rate.TuesDayRate;
            driverRate.TuesNightRate = entity.Rate.TuesNightRate;
            driverRate.WedDayRate = entity.Rate.WedDayRate;
            driverRate.WedNightRate = entity.Rate.WedNightRate;
            driverRate.ThursDayRate = entity.Rate.ThursDayRate;
            driverRate.ThursNightRate = entity.Rate.ThursNightRate;
            driverRate.FriDayRate = entity.Rate.FriDayRate;
            driverRate.FriNightRate = entity.Rate.FriNightRate;
            driverRate.SatDayRate = entity.Rate.SatDayRate;
            driverRate.SatNightRate = entity.Rate.SatNightRate;
            driverRate.SunDayRate = entity.Rate.SunDayRate;
            driverRate.SunNightRate = entity.Rate.SunNightRate;

            return Driver.AddDriver(driver);
        }

        public static List<Web.Business.DTO.DriverCommentEntity> GetDriverComments(int DriverId)
        {
            List<MTC.Common.DTO.DriverComment> driversComment;

            if (DriverId == 0)
                return null;

            driversComment = Driver.GetDriverComment(DriverId);
            if (driversComment == null)
                return null;

            List<DriverCommentEntity> _driverCommentEntityList = new List<DriverCommentEntity>();

            foreach (MTC.Common.DTO.DriverComment driverComment in driversComment)
            {
                DriverCommentEntity entity = new DriverCommentEntity();
                entity.DriverId = driverComment.DriverId;
                entity.Comment = driverComment.Comment;
                entity.CommentDate = driverComment.CommentDate;
                entity.DriverCommentId = driverComment.DriverCommentId;

                _driverCommentEntityList.Add(entity);
            }
            return _driverCommentEntityList;            
        }

        public static int AddComment(int driverId, DateTime commentDate, string comment)
        {
            return Driver.AddComment(driverId, commentDate, comment);
        }

        public static bool UpdateComment(int driverCommentId, int driverId, DateTime commentDate, string comment)
        {
            return Driver.UpdateDriverComment(driverCommentId, driverId, comment, commentDate );
        }

        public static bool DeleteComment(int driverCommentId)
        {
            return Driver.DeleteDriverComment(driverCommentId);
            
        }
    }
}
